#include "unicode.h"
#include "parser.h"

#define DEBUG_STOCK_CODE

static char *cn_shanghai = "上海A股";
static char *cn_shenzhen = "深圳A股";
static char *cn_zhongxiao = "中小板块";

static int init_flag = 0;
static int shanghai_num, shenzhen_num, zhongxiao_num;
struct stock_code sh_stock[2048], sz_stock[2048], zx_stock[2048];

int parser_stock(char *buffer, int size, char *string, struct stock_code *sc, int *page)
{
	char *p1, *p2;
	int len, count;
	char unicode[256], gb2312[256];
    if(!buffer || !string || !sc)
        return -1;
	len = Utf8ToUnicode((unsigned short *)unicode, string, -1);
	len = unicode_to_gb2312(gb2312, 256, unicode, len*2);
	gb2312[len] = 0;
	p1 = strstr(buffer, gb2312);
    if(!p1)
        return -1;
	p1 += strlen(gb2312);
	p1 = strstr(p1, gb2312);
    if(!p1)
        return -1;

    count = 0;
    do {
        p2 = strstr(p1, "href=\"/cgi-bin/sstock/ggcx?zhengquandm=");
        if(!p2)
            break;
		p2 += strlen("href=\"/cgi-bin/sstock/ggcx?zhengquandm=");
        memcpy(sc->code, p2, 6);
        p1 = strstr(p2, "\n");
        p1 = strstr(p1, ">");
        p1++;
        p2 = strstr(p1, "<");
        memcpy(gb2312, p1, p2 - p1);
        len = gb2312_to_unicode(unicode, 256, gb2312, p2 - p1);
        len = UnicodeToUtf8(sc->name, (unsigned short *)unicode, len/2, NULL);
		count++;
		sc++;
    } while(1);

	if(page)
	{
		int num = 0;
		p1 = strstr(p1, "<script>write_pdown(");
		p1 += strlen("<script>write_pdown(");
		if(p1)
			num = atoi(p1);
		*page = num;
	}
	return count;
}

static char temp_sh_url[256];
static char *gen_shanghai_url(int index)
{
	strcpy(temp_sh_url, "http://stock.finance.qq.com/hqing/hqst/paiminglist");
	sprintf(temp_sh_url+strlen(temp_sh_url), "%d.htm", index);
	return temp_sh_url;
}

static char temp_sz_url[256];
static char *gen_shenzhen_url(int index)
{
	strcpy(temp_sz_url, "http://stock.finance.qq.com/hqing/hqst/paiminglistsa");
	sprintf(temp_sz_url+strlen(temp_sz_url), "%d.htm", index);
	return temp_sz_url;
}

static char temp_zx_url[256];
static char *gen_zhongxiao_url(int index)
{
	strcpy(temp_zx_url, "http://stock.finance.qq.com/hqing/hqst/paiminglistbk");
	sprintf(temp_zx_url+strlen(temp_zx_url), "%d.htm", index);
	return temp_zx_url;
}

void init_shanghai_stock(void)
{
	int i, page, num, ret;
	char *p;
	struct download_task task;
	
	i = 0;
	num = 0;
	do 
	{
		p = gen_shanghai_url(++i);
		init_download_task(&task, p);
		ret = download(&task);
		num += parser_stock(task.buffer, task.size, cn_shanghai, &sh_stock[num], &page);
		free_download_task(&task);
	} while(i < page);
	shanghai_num = num;
	{
		for(i = 0; i < num; i++)
		{
#ifdef DEBUG_STOCK_CODE
			printf("%s  %s\n", sh_stock[i].code, sh_stock[i].name);
#endif
		}
	}
#ifdef DEBUG_STOCK_CODE
	printf("num = %d, page = %d\n", shanghai_num, page);
#endif
}

void init_shenzhen_stock(void)
{
	int i, page, num, ret;
	char *p;
	struct download_task task;
	
	i = 0;
	num = 0;
	do 
	{
		p = gen_shenzhen_url(++i);
		init_download_task(&task, p);
		ret = download(&task);
		num += parser_stock(task.buffer, task.size, cn_shenzhen, &sz_stock[num], &page);
		free_download_task(&task);
	} while(i < page);
	shenzhen_num = num;
	{
		for(i = 0; i < num; i++)
		{
#ifdef DEBUG_STOCK_CODE
			printf("%s  %s\n", sz_stock[i].code, sz_stock[i].name);
#endif
		}
	}
#ifdef DEBUG_STOCK_CODE
	printf("num = %d, page = %d\n", shenzhen_num, page);
#endif
}

void init_zhongxiao_stock(void)
{
	int i, page, num, ret;
	char *p;
	struct download_task task;
	
	i = 0;
	num = 0;
	do 
	{
		p = gen_zhongxiao_url(++i);
		init_download_task(&task, p);
		ret = download(&task);
		num += parser_stock(task.buffer, task.size, cn_zhongxiao, &zx_stock[num], &page);
		free_download_task(&task);
	} while(i < page);
	zhongxiao_num = num;
	{
		for(i = 0; i < num; i++)
		{
#ifdef DEBUG_STOCK_CODE
			printf("%s  %s\n", zx_stock[i].code, zx_stock[i].name);
#endif
		}
	}
#ifdef DEBUG_STOCK_CODE
	printf("num = %d, page = %d\n", zhongxiao_num, page);
#endif
}

struct stock_code *get_shanghai_stock(index)
{
	if(!init_flag)
	{
		init_shanghai_stock();
		init_shenzhen_stock();
		init_zhongxiao_stock();
		init_flag = 1;
	}
	if(index >= shanghai_num)
		return NULL;
	return &sh_stock[index];
}

struct stock_code *get_shenzhen_stock(index)
{
	if(!init_flag)
	{
		init_shanghai_stock();
		init_shenzhen_stock();
		init_zhongxiao_stock();
		init_flag = 1;
	}
	if(index >= shenzhen_num)
		return NULL;
	return &sz_stock[index];
}

